<template>
	<n-tag 	v-bind="$attrs" > {{ tagVal }} </n-tag>
</template>

<script  setup lang='ts' >
import { fetchDataListByType } from '@/service/api/sys/dict/data'
import { onMounted, ref } from 'vue';
import { SysDictData } from '~/src/views/system/dict/dict-data';

type Props = {
	type:string,
	dictList?:SysDictData[],
	value?:string,
	default?:string
}
const props = withDefaults(defineProps<Props>(),{
	default:''
})
const { type,value,dictList } = props

const tagVal = ref<string|undefined>(props.default)

async function initType(){
	let list:SysDictData[] = [];
	if(dictList){
		list = dictList;
	}else{
		const { data } = await fetchDataListByType(type)
		list = data || []
	}
	const result = list.find(it=>it.value === value)
	tagVal.value = result ? result.label : props.default
}


defineOptions({ name: 'SDictTag' });
onMounted(() => {
	initType()
})
</script>

<style lang='scss' scoped>

</style>
